package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {
    //// 私有化构造器, 不然new 对象
    //private UserContext(){};
    //
    //public static final String USER_IN_SESSION = "USER_IN_SESSION";
    //public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";
    //
    //// 获取Session
    //public static HttpSession getSession() {
    //    ServletRequestAttributes reqAttrs = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
    //    return reqAttrs.getRequest().getSession();
    //}
    //
    //// 往session存入登录用户
    //public static void setCurrentUser(Employee employee) {
    //    getSession().setAttribute(USER_IN_SESSION, employee);
    //}
    //// 从session获取登录用户
    //public static Employee getCurrentUser() {
    //    return (Employee) getSession().getAttribute(USER_IN_SESSION);
    //}
    //
    //// 往session存入登录用户的权限信息
    //public static void setExpressions(List<String> permission) {
    //    getSession().setAttribute(PERMISSION_IN_SESSION, permission);
    //}
    //// 从session获取登录用户的权限信息
    //public static List<String> getExpressions() {
    //    return (List<String>) getSession().getAttribute(PERMISSION_IN_SESSION);
    //}


    /**
     * 从shiro中取当前登录用户
     */
    public static Employee getCurrentUser() {
        // 先拿主体, 再拿身份信息
        return (Employee) SecurityUtils.getSubject().getPrincipal();
    }
}
